/*
 * NativeFileParts.java  0.4.0 / Nov 5, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Native File Parts.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class NativeFileParts implements NativeFilePart {

// Private Fields. -------------------------------------------------------------

  private final ArrayList parts;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new NativeFileParts object.
   */
  public NativeFileParts() {
    parts = new ArrayList();
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Adds native file part.
   *
   * @param part NativeFilePart
   */
  public void add(NativeFilePart part) {
    parts.add(part);
  }

  /**
   * Returns native file part at specified index.
   *
   * @param index the index
   *
   * @return native file part at specified index
   */
  public NativeFilePart get(int index) {
    return (NativeFilePart)parts.get(index);
  }

  /**
   * Removes native file part at specified index.
   *
   * @param index the index
   *
   * @return native file part at specified index
   */
  public NativeFilePart remove(int index) {
    return (NativeFilePart)parts.remove(index);
  }

  /**
   * Removes native file part.
   *
   * @param part the native file part
   *
   * @return <tt>true</tt> if the collection contained the specified part.
   */
  public boolean remove(NativeFilePart part) {
    return parts.remove(part);
  }

  /**
   * Returns the number of the parts.
   *
   * @return the number of the parts
   */
  public int size() {
    return parts.size();
  }

  /**
   * Returns iterator over the parts.
   *
   * @return iterator over the parts
   */
  public Iterator iterator() {
    return parts.iterator();
  }

// NativeFilePart Interface Implementation. ------------------------------------

  /**
   * {@inheritDoc}
   */
  public void store(PrintWriter pw) throws IOException {
    ArrayList parts = this.parts;
    for (int i = 0; i < parts.size(); i++) {
      NativeFilePart nativeFilePart = (NativeFilePart)parts.get(i);
      nativeFilePart.store(pw);
    }
  }

}
